﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyCommon.HttpContextUser;
using MyIServices;
using MyModels;
using MyModels.Models;
using System.Linq.Expressions;

namespace MyApi.Controllers
{
    /// <summary>
    /// 模块控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize("Permission")]
    public class ModuleController : BaseApiController
    {
        private readonly IModuleServices _moduleServices;
        private readonly IUser _user;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="moduleServices"></param>
        public ModuleController(IModuleServices moduleServices, IUser user)
        {
            _moduleServices = moduleServices;
            _user = user;
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<Modules>>> Get(int page = 1, int pageSize = 10, string key = "")
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key)) key = "";

            Expression<Func<Modules, bool>> expression = a => a.IsDeleted != true && (a.Name != null && a.Name.Contains(key));

            PageModel<Modules> data = new PageModel<Modules>();
            if (page == -1)
            {
                var list = await _moduleServices.Query(expression, " Id desc ");
                data.Data = list;
            }
            else
            {
                data = await _moduleServices.QueryPage(expression, page, pageSize, " Id desc ");
            }
            return Success(data, "获取成功");

            //var list = await _moduleServices.Query();
            //return Ok(list);
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Post(Modules module)
        {
            module.CreateId = _user.UserId;
            module.CreateBy = _user.UserName;
            var id = await _moduleServices.Add(module);
            return id > 0 ? Success(id.ToString(), "添加成功") : Fail();
        }

        /// <summary>
        /// 更新菜单
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<MessageModel<string>> Put(Modules module)
        {
            if (module == null|| module.Id<=0) return Fail("缺少参数");
            var id = await _moduleServices.Update(module);
            return id ? Success(id.ToString(), "更新成功") : Fail();
        }
        
        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<MessageModel<string>> Delete(long id)
        {
            if (id <= 0) return Fail("缺少参数");
            var model = await _moduleServices.QueryById(id);
            if (model == null) return Fail("信息不存在");
            model.IsDeleted = true;
            var res = await _moduleServices.Update(model);
            return res ? Success(id.ToString(), "删除成功") : Fail();
        }
    }
}
